<?php
class My_API_ss14_Users
{
    private $_config;

    public function __construct($gameId = null)
    {
        if(!isset($gameId) || !file_exists(CONFIG_PATH2 . "/$gameId.ini")){
            return false;
        }
        
        $config = new Zend_Config_Ini(CONFIG_PATH2 . "/$gameId.ini",APPLICATION_ENV);
        $this->_config = $config;
    }
    
    public function getUsersByServerId($listUser, $serverId){
        
		$i = 0;
		$infoUsers = array();
        
		$userCheck = array_chunk($listUser, 50);
		foreach ($userCheck as $key => $listCheck){				
			//get qid of listuser		
			$users 		= str_replace("'", "", implode(",", $listCheck));				
			$secret 	= $this->_config->getUserInfo->secret;
			$agentId 	= urlencode($this->_config->getUserInfo->agentId);
			$agentKey	= urlencode($this->_config->getUserInfo->agentKey);
			$sign		= urlencode(md5($agentKey . md5($users) . $secret));
			$urlGetQid 	= $this->_config->getUserInfo->urlGetUserId . "?argument0={$agentId}&argument1={$agentKey}&argument2={$users}&argument3={$sign}"; 				
			$qids 		= json_decode(file_get_contents($urlGetQid));	
			if($qids->status == 1){
				$qids = (array)$qids->result;
                foreach($qids as $username=>$userId){
                    $arrUserId[$userId] = $username;
                }
                $serverIdUpper = strtoupper($serverId);
				$qidsCheck	= implode(",", $qids);
				$url = $this->_config->getUserInfo->url;
				$url .= "?server_id={$serverIdUpper}&qids={$qidsCheck}";
				$result = file_get_contents($url);
				$result = json_decode($result);
                
				if(count($result) > 0){
					foreach($result as $info){
                        
    					$infoUsers[$i]['serverId'] = $serverId;
    					$infoUsers[$i]['roleName'] = $info->name;
    					$infoUsers[$i]['roleLevel'] = $info->level;
                        
    					$i++;
					}
				}else{
					continue;
				}
			}else{
				continue;
			}
		}
		return $infoUsers;
    }
    
    public function getUsernameByRolename($listRoleName, $serverId){
        
		$i = 0;
		$infoUsername = array();
        
		$userCheck = array_chunk($listRoleName, 50);
		foreach ($userCheck as $key => $listCheck){	
			//get qid of listuser		
            $serverIdUpper = strtoupper($serverId);
            $serverIdLower = strtolower($serverId);
			$users = str_replace("'", "", implode(",", $listCheck));
            $sign = md5($serverIdUpper.$users.$this->_config->getUsernameInfo->key);
			$url = "http://".$serverIdLower.$this->_config->getUsernameInfo->url;
			$url .= "?server_id={$serverIdUpper}&rolename={$users}&sign=$sign";
			$result = json_decode(file_get_contents($url));
            
            if(!isset($result->status) || $result->status != 1){
                continue;
            }
            
            foreach($result->result as $key2=>$value2){
                if(!isset($value2->username)){
                    continue;
                }
                $qid = $value2->username;
                
                $secret 	= $this->_config->getUsernameInfo->secret;
    			$agentId 	= urlencode($this->_config->getUsernameInfo->agentId);
    			$agentKey	= urlencode($this->_config->getUsernameInfo->agentKey);
    			$sign		= urlencode(md5($agentKey . md5($qid) . $secret));
    			$urlGetQid 	= $this->_config->getUsernameInfo->urlGetUserId . "?argument0={$agentId}&argument1={$agentKey}&argument2={$qid}&argument3={$sign}"; 				
    			$username 	= json_decode(file_get_contents($urlGetQid));
                if(!isset($username->result) || !$username->result){
                    continue;
                }
                
                $infoUsername[] = array(
                        'account' => $key2,
                        'username' => $username->result,
                        'roleLevel' => $value2->level
                    );
            }
		}
        
		return $infoUsername;
    }
}